package com.cx.controller.czw;

import com.cx.interceptor.Status;
import com.cx.pojo.Admin;
import com.cx.pojo.User;
import com.cx.redis.RedisOpsUtil;
import com.cx.service.czw.LoginService;
import com.cx.util.czw.ImageCodeUtil;
import com.cx.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;


/**
 * 丛志文
 */
@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    LoginService loginService;
    @Autowired
    RedisOpsUtil redisOpsUtil;

    //表单提交过来的路径
    @RequestMapping("userLogin")
    @ResponseBody
    public ResponseResult userLogin(String phone, String pwd){
        ResponseResult responseResult = new ResponseResult();
        System.out.println(phone);
        System.out.println(pwd);
        //调用service方法
        User user = loginService.userLogin(phone,pwd);
        //若有user则添加到model里并且跳转到成功页面
        if(null != user){
            responseResult.setMessage("登录成功");
            responseResult.setCode(1);
            Status.map.put("phone",phone);
            Status.map.put("login","1");
            Status.map.put("id",user.getUserId());
//            redisOpsUtil.set("phone",phone);
//            redisOpsUtil.set("login","1");
//            redisOpsUtil.set("id",String.valueOf(user.getUserId()));
        }else {
            responseResult.setMessage("登录失败");
            responseResult.setCode(2);
        }
        return responseResult;
    }

    //生成图片验证码
    @RequestMapping("getImgCode")
    @ResponseBody
    public void getImgCode(HttpServletResponse response) throws IOException {
        /*
             1.生成验证码
             2.把验证码上的文本存在session中
             3.把验证码图片发送给客户端
             */
        ImageCodeUtil ivc = new ImageCodeUtil();     //用我们的验证码类，生成验证码类对象
        BufferedImage image = ivc.getImage();  //获取验证码
        Status.map.put("code",ivc.getText());
//        redisOpsUtil.set("code",ivc.getText());
        System.out.println(image);
        ivc.output(image, response.getOutputStream());//将验证码图片响应给客户端
    }

    //通过短信登录进行验证判断
    @RequestMapping("userLoginByPhoneCode")
    @ResponseBody
    public ResponseResult userLoginByCode(String phoneCode , String code ,String phone){
        ResponseResult responseResult = new ResponseResult();
        User user = loginService.userLoginByCode(phone);
        System.out.println(user);
        System.out.println(code);
        System.out.println(phoneCode);
        //进行手机号存不存在判断
        if(null !=user) {
            //先进行验证码的判断
            if (Status.map.get("code").equals(code)) {
                //再进行短信验证码的判断
                if (Status.map.get("randomPhoneCode").equals(phoneCode)) {
                    responseResult.setMessage("登录成功");
                    responseResult.setCode(1);
                    Status.map.put("login","1");
                    Status.map.remove("code");
                    Status.map.remove("randomPhoneCode");
//                    redisOpsUtil.set("login","1");
//                    redisOpsUtil.del("code","randomPhoneCode");
                } else {
                    //短信验证失败
                    responseResult.setMessage("短信验证码错误");
                    responseResult.setCode(3);
                }
            }else{
                //验证码错误
                responseResult.setMessage("验证码错误");
                responseResult.setCode(4);
                }
            } else {
               responseResult.setMessage("手机号不存在");
               responseResult.setCode(2);
            }

        return responseResult;
    }

    @RequestMapping("adminLogin")
    @ResponseBody
    public ResponseResult adminLogin(String phone ,String pwd){
        ResponseResult responseResult = new ResponseResult();
        System.out.println(phone);
        System.out.println(pwd);
        Admin admin = loginService.adminLogin(phone,pwd);
        if(null != admin){
            responseResult.setMessage("登录成功");
            responseResult.setCode(1);
            Status.map.put("phone",phone);
            Status.map.put("login","2");
            Status.map.put("id",admin.getAdminId());
//            redisOpsUtil.set("phone",phone);
//            redisOpsUtil.set("login","2");
//            redisOpsUtil.set("id",admin.getAdminId());
        }else {
            responseResult.setMessage("登录失败");
            responseResult.setCode(2);
        }
        return responseResult;
    }
}
